package com.ls.service.impl;

import com.ls.dao.ContractDao;
import com.ls.entity.Contract;
import com.ls.entity.JsonResult;
import com.ls.entity.Page;
import com.ls.service.IContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;



@Service("contractService")
public class ContractServiceImpl implements IContractService {
	@Autowired
	ContractDao dao;

	public JsonResult<List<Contract>> findContract() {

		JsonResult<List<Contract>> result = new JsonResult<List<Contract>>();
		List<Contract> list = dao.findContract();
		if (list != null) {
			result.setStatus(0);
			result.setData(list);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询合同出错");
		return result;
	}

	public JsonResult<List<Contract>> findContractFenye(int pageId, int num) {
		JsonResult<List<Contract>> result = new JsonResult<List<Contract>>();
		Page page = new Page();
		int jilu = dao.findContract().size();
		List<Contract> list = dao.findContractFenye((pageId - 1) * num, num);
		if (list != null) {
			page.setPageId(pageId);
			page.setPageNum(jilu % num == 0 ? jilu / num : jilu / num + 1);
			result.setStatus(0);
			result.setData(list);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询合同分页出错");
		return result;
	}

	public JsonResult<Object> deleteByIdCo(String[] contract_id) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.deleteByIdCo(contract_id);
		// 失败
		if (i == 0) {
			result.setStatus(1);
			result.setMsg("删除合同失败");
			return result;
		}
		result.setStatus(0);
		result.setData(i);
		return result;
	}

	public JsonResult<List<Contract>> findName(String name, int pageId, int num) {
		JsonResult<List<Contract>> result = new JsonResult<List<Contract>>();
		Page page = new Page();
		int jilu = dao.findName(name).size();
		List<Contract> list = dao.findNameFenye(name, (pageId - 1) * num, num);
		if (list != null) {
			page.setPageId(pageId);
			page.setPageNum(jilu % num == 0 ? jilu / num : jilu / num + 1);
			result.setStatus(0);
			result.setData(list);
			result.setPage(page);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询失败");
		return result;
	}

	public JsonResult<Object> insertContract(Contract contract) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.insertContract(contract);
		if (i==1) {
			result.setStatus(0);
			result.setData(i);
			return result;
		}
		result.setStatus(1);
		result.setMsg("添加合同失败了");
		return result;
	}

	public JsonResult<Contract> showCo(int contract_id) {
		JsonResult<Contract> result = new JsonResult<Contract>();
		Contract contract = dao.showCo(contract_id);
		if (contract!=null) {
			result.setStatus(0);
			result.setData(contract);
			return result;
		}
		result.setStatus(1);
		result.setMsg("show合同失败了");
		return result;
	}

	public JsonResult<Object> updateContract(Contract contract) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.update(contract);
		if (i==1) {
			result.setStatus(0);
			result.setData(i);
			return result;
		}
		result.setStatus(1);
		result.setMsg("修改合同失败了");
		return result;
	}



}
